<template>
  <div class="seckill"
    v-infinite-scroll="loadMore"
    infinite-scroll-disabled="loading"
    infinite-scroll-distance="10">
    <shop-header>  
      <h2>限时秒杀</h2>  
    </shop-header>
    <div class="hours-wrapper">
      <ul class="hours-list">
        <li class="hours-item" v-if="index < 5" v-for="(item,index) in timeArr" :class="{ active: nowIndex == index }" @click="toggleHours(index)">
          <p class="hours">{{ item.hours }}:00</p>
          <span>{{ item.text }}</span>
        </li>
      </ul>
    </div>
    <div class="list-head bottom-1px">
      <span class="seckill-text">秒杀中 先下单先得哦</span>
      <div class="time">
        <span class="static-txt-end">距结束</span>
        <clocker v-if="timeArr.length > 0" class="seckill-time" :time="timeArr[1].date">
          <span>%H</span> : <span>%M</span> : <span>%S</span>
        </clocker>
      </div>
    </div>
    <div class="seckill-list-wrapper">
      <router-link v-for="item in seckillList" :key="item.id" class="seckill-item bottom-1px" :to="{ path: '/goods/detail', query: { id: item.goodsId } }" tag="div">
        <div class="item-container">
          <div class="seckill-cover">
            <span class="img-holder"><img class="obj-cover" :src="item.goodsCover"></span>
          </div>
          <div class="content">
            <h3 class="product-name">{{ item.goodsName }}</h3>
            <div class="price">
              <span class="price_val"><i>¥</i>{{ item.seckillPrice | toDecimal }}</span>
              <span class="skill-count">去秒杀</span>
            </div>
            <div class="skill-price">
              <span class="old-price">¥{{ item.price | toDecimal }}</span>
              <div class="skill-lod">
                <span class="sale-count">已秒{{ item.seckillRatio }}%</span>
                <div class="skill-progress">
                  <div class="skill-pro-bg">
                    <p class="skill-iteam-progress">
                      <span class="skill-pro-insetbg">
                        <span class="skill-iteam-pro" :style="{ width: item.seckillRatio+'%' }"></span>
                      </span> 
                    </p>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </router-link>
    </div>
    <div class="spinner-wrapper">
      <mt-spinner v-show="loading && seckillList.length > 0" class="triple-bounce" type="triple-bounce"></mt-spinner>
      <load-more v-show="nomore && seckillList.length > 0" :show-loading="false" tip="暂无更多数据" background-color="#f5f5f5"></load-more>
    </div>
    <no-results :height="{ height: '100%' }" v-if="seckillList.length == 0"></no-results>
  </div>
</template>

<script>
import { Clocker, LoadMore } from 'vux'
import { getNowTime, toDecimal } from '@/utils/mUtils'
import ShopHeader from '@/components/public/ShopHeader'
import Split from '@/components/public/Split'
import http from '@/utils/http'
import NoResults from '@/components/public/NoResults'

export default {
	components: {
    ShopHeader,
    Clocker,
    NoResults,
    LoadMore
	},
  data () {
    return {
      nowIndex: 0,
      timeArr: [],
      page: 1,
      limit: 5,
      loading: true,
      nomore: false,
      seckillList: []
    }
  },
  methods: {
    toggleHours (index) {
      this.nowIndex = index
      this.seckillList = []
      this.page = 1
      this.getSeckillList()
    },
    loadMore () {     
      if (!this.nomore) {
        this.loading = true
        setTimeout(() => {
          this.getSeckillList()
        }, 1500)
      }
    },
    getSeckillList () {
      // 获取秒杀的时间
      this.timeArr = getNowTime()
      let params = {
        startTime: this.timeArr[this.nowIndex].date / 1000,
        endTime: this.timeArr[this.nowIndex + 1].date / 1000,
        page: this.page,
        limit: this.limit
      }
      http.get('/api/v1/getSeckillList', params, false, res => {
        if (res.status == 1 && res.data.length > 0) {
          this.seckillList = this.seckillList.concat(res.data)
          this.seckillList.map(item => {
            item.seckillRatio = parseInt(item.salesVolume / item.seckillStock * 100)
            return item
          })
          this.page ++
          this.nomore = false
          this.loading = false
          if (res.data.length < this.limit) {
            this.nomore = true
          }
        }else {
          this.loading = false
          this.nomore = true
          return
        }
      })
    }
  },
  mounted () {
    this.getSeckillList()
  },
  filters: {
    toDecimal (val) {
      return toDecimal(val)
    }
  }
}
</script>

<style scoped>
.seckill-list-wrapper {
  background: #fff;
}
.seckill-item {
  position: relative;
}
.item-container {
  display: flex;
  padding: 15px;
  position: relative;
}
.seckill-item .content {
  flex: 1;
}
.seckill-cover {
  flex: 0 0 90px;
  width: 90px;
  height: 90px;
  margin-right: 8px;
}
.img-holder {
  padding-bottom: 100%;
}
.product-name {
  font-size: 14px;
  color: #333;
  line-height: 20px;
  height: 40px;
  overflow: hidden;
  display: -webkit-box;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.price {
  display: flex;
  align-items: center;
  line-height: 20px;
  padding-top: 8px;
}
.price_val {
  flex: 1;
  color: #e4393c;
  font-size: 18px;
}
.price_val i {
  font-size: 10px;
  font-style: normal;
  display: inline-block;
  padding-right: 2px;
}
.skill-count {
  width: 72px;
  height: 25px;
  line-height: 25px;
  background-color: #f23030;
  font-size: 14px;
  text-align: center;
  color: #fff;
  -webkit-border-radius: 2px;
  border-radius: 2px;
}
.skill-price {
  position: relative;
  height: 15px;
  overflow: hidden;
  font-size: 12px;
}
.old-price {
  font-size: 12px;
  color: #848689;
  text-decoration: line-through;
}
.skill-lod {
  position: absolute;
  right: 0;
  top: 0;
}
.sale-count {
  font-size: 10px;
  color: #848689;
  display: inline-block;
}
.skill-progress {
  width: 72px;
  margin-left: 4px;
  height: 8px;
  overflow: hidden;
  display: inline-block;
}
.skill-pro-bg {
  background: rgba(255,255,255,.8);
  height: 7px;
  width: 100%;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
.skill-iteam-progress {
  position: relative;
  top: 0;
  left: 0;
  height: 15px;
  width: 200%;
  -webkit-transform: scale(.5,.5);
  transform: scale(.5,.5);
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
  border: solid 1px #f36262;
  border-radius: 50px;
  overflow: hidden;
}
.skill-pro-insetbg {
  width: 100%;
  height: 100%;
  display: inline-block;
  border: solid 1px rgba(255,255,255,.2);
}
.skill-iteam-pro {
  position: absolute;
  top: 0;
  left: -1px;
  background-image: linear-gradient(to right,#ff8989,#f67373);
  background-image: -webkit-linear-gradient(right,#ff8989,#f67373);
  background: -webkit-gradient(linear,left center,right center,from(#ff8989),to(#f67373));
  background: -moz-linear-gradient(right,#ff8989,#f67373);
  background: -o-linear-gradient(right,#ff8989,#f67373);
  height: 100%;
  display: block;
  width: 0;
  -webkit-transition: width 1.8s;
  transition: width 1.8s;
}
.hours-wrapper {
  height: 50px;
}
.hours-list {
  position: fixed;
  top: 46px;
  z-index: 98;
  width: 100%;
  max-width: 640px;
  background: #fff;
  display: flex;
}
.hours-item {
  flex: 1;
  text-align: center;
  padding: 8px 0;
}
.hours-item.active {
  color: #f23030;
}
.hours-item p{
  font-size: 17px;
  line-height: 23px;
  font-weight: 700;
}
.hours-item.active p {
  font-size: 23px;
}
.hours-item span {
  font-size: 11px;
  line-height: 11px;
  display: block;
}
.list-head {
  display: flex;
  align-items: center;
  background: #fff;
  padding: 5px 15px 10px 15px;
}
.list-head:after {
  bottom: 0;
}
.seckill-text {
  flex: 1;
  color: #f23030;
  font-size: 13px;
  line-height: 16px;
}
.time {
  position: relative;
  margin-top: -4px;
  height: 16px;
  font-size: 0;
}
.static-txt-end,
.seckill-time {
  display: inline-block;
  vertical-align: middle;
  font-size: 12px;
  line-height: 12px;
}
.static-txt-end {
  color: #333;
  margin-right: 5px;
}
.seckill-time span {
  font-size: 12px;
  display: inline-block;
  background-color: #000;
  width: 16px;
  height: 16px;
  color: #fff;
  font-size: 10px;
  line-height: 18px;
  text-align: center;
  border-radius: 3px;
  -webkit-border-radius: 3px;
}
</style>
